<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
<!--    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>-->
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/home.css" />
</head>
<body>

<div class="home-box">
    <input type="button" style="position: absolute;left: 20px" value="退出登录" onclick="logout()">
    <h1>登录成功</h1>
    <button>上次在线时间</button>
    <input type="text" id="time" disabled="disabled">
    <h1>上传文件到fmy的电脑</h1>
    <form id="upload_form" action="/upload" enctype="multipart/form-data" method="post">
        <input id="upload_file" type="file" name="uploadFile" style="display: inline-block"  class="upload-btn">
        <!--            <input id="upload_file2" type="file" name="uploadFile" style="display: inline-block">-->

        <input id="upload_submit" type="button" value="Upload">
    </form>
</div>

</body>
<script>
    var timer = null;
    $(document).ready(function () {
        $("#time").val(getCookie("last_time"));
        update();
        timer = setInterval(update,5*60*1000);
    })
    function toIndex() {
        window.open('/index');
    }
    function update() {
        var userinfo = {
            'name' : getCookie("name"),
            'password' : getCookie("password")
        };
        $.ajax({
                url:'/update',
                type:'POST',
                dataType:'json',
                contentType:'application/json',
                data:JSON.stringify(userinfo),
                success:function (data) {
                    $("#time").val(getCookie("last_time"));
                }
            }
        )
    }
    function logout() {
        var userinfo = {
            'name' : getCookie("name"),
            'password' : getCookie("password")
        };
        $.ajax({
                url:'/logout',
                type:'POST',
                dataType:'json',
                contentType:'application/json',
                data:JSON.stringify(userinfo),
                success:function (data) {
                    clearInterval(timer);
                    clearAllCookie();
                    toIndex();
                }
            }
        )
    }
    function getCookie(name) {
        var allcookies = document.cookie;
        var cookie_pos = allcookies.indexOf(name);
        if (cookie_pos != -1) {
            cookie_pos += name.length + 1;
            var cookie_end = allcookies.indexOf(';', cookie_pos);
            if (cookie_end == -1) {
                cookie_end = allcookies.length;
            }
            var value = unescape(allcookies.substring(cookie_pos, cookie_end))
        }
        return value;
    }

    function clearAllCookie() {
        var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        var date = new Date();
        date.setTime(date.getTime() - 1);
        if (keys) {
            for (var i = keys.length; i--;) {
                document.cookie = keys[i] + '=0;expires' + date.toUTCString();
            }
        }
    }
    $("#upload_submit").click(function () {
        var upload_file = document.getElementById("upload_file");
        if (upload_file.value == '') {
            alert('请选择文件');
            return;
        } else if (upload_file.files[0].size > 1024 * 1024 * 100) {
            alert('文件太大');
            return;
        }
        var form = document.getElementById("upload_form");
        form.submit();
    })
</script>
</html>
